package com.joker.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.joker.common.core.utils.StringUtils;
import com.joker.common.redis.service.RedisService;
import com.joker.order.config.RabbitMQConfig;
import com.joker.order.constant.OrderConstant;
import com.joker.order.domain.dto.OrderDTO;
import com.joker.order.mapper.OrderMapper;
import com.joker.order.service.OrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisService redisService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    @Qualifier(value = "lua-inventory")
    private DefaultRedisScript<Long> luaInventoryNum;

    @Override
    public void takeOrder(OrderDTO orderDTO) {

        // 设置订单状态为处理中
        redisService.setCacheObject(OrderConstant.ORDER+orderDTO.getOrderNo(), OrderConstant.ORDER_STATUS_PROGRESS);

        rabbitTemplate.convertAndSend(RabbitMQConfig.ORDER_QUEUE, JSONObject.toJSONString(orderDTO)); // 发送异步消息

    }

    @Override
    public Long skuInventory(OrderDTO orderDTO) {
        RedisTemplate redisTemplate = redisService.redisTemplate;

        List<String> keys = Arrays.stream(orderDTO.getSkuIds())
                .map(Object::toString)
                .collect(Collectors.toList());

        Long execute = (Long) redisTemplate.execute(luaInventoryNum, keys, orderDTO.getCounts());

        return execute;
    }

    @Override
    public void createOrder(OrderDTO orderDTO) {

        orderDTO.setUserId(1L);

        orderDTO.setOrderStatus(OrderConstant.ORDER_STATUS_FINISH);

        orderDTO.setPayStatus(1L);

        orderMapper.createOrder(orderDTO);

        Long[] skuIds = orderDTO.getSkuIds();
        orderMapper.insertOrderSku(orderDTO.getId(), skuIds);

    }

    @Override
    public Long selectOrderStatus(String orderNo) {

        Object cacheObject = redisService.getCacheObject(OrderConstant.ORDER + orderNo);
        return (Long) cacheObject;

    }

    @Override
    public void updateOrderStatusByOrderNo(String orderNo) {
        if (StringUtils.isEmpty(orderNo)){
            throw new RuntimeException("没有该订单");
        }
        orderMapper.updateOrderStatusByOrderNo(orderNo);
    }


    @Scheduled(cron = "* * 1 * * ?")
    public void updateOrderStatus() {

        List<Long> skuIds = orderMapper.selectSkuIds();

        for (Long skuId : skuIds) {
            Object inventory = redisService.getCacheObject("inventory:" + skuId);
            if (inventory != null){
                orderMapper.updateInventory(skuId, (Integer) inventory);
            }
        }

    }

}
